﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace HotelManagement
{
    public partial class PaymentFRM : Form
    {
        public int rs_id;
        public int f_id;

        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        SqlDataReader dr;

        public PaymentFRM()
        {
            InitializeComponent();
        }

        private void CustomerSearchBTN_Click(object sender, EventArgs e)
        {
            ReserveSerach rsf = new ReserveSerach();
            rsf.ShowDialog();
            rs_id = rsf.Rs_ID;

            SqlCommand SelectTPrice = new SqlCommand("select f_id,(f_tprice - IsNull((f_payment),0)) from Factor where f_rs_id = @f_rs_id", Cnn);
            SelectTPrice.Parameters.AddWithValue("@f_rs_id", rs_id);
            Cnn.Open();
            dr = SelectTPrice.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                RemainingAmountLbl.Text ="مبلغ فابل پرداخت "+ dr[1].ToString() + "ریال";
                f_id = (int)dr[0];
            }
            else
            {
                MessageBox.Show("هزینه ای ثبت نشده است !!!");
            }

            Cnn.Close();



            SqlCommand SelectTotalPayment = new SqlCommand("select ISNULL(SUM(p_amount),0) from Payment WHERE p_f_id=@p_f_id", Cnn);
            SelectTotalPayment.Parameters.AddWithValue("@p_f_id", f_id);
            Cnn.Open();
            dr = SelectTotalPayment.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                TotalPayedLbl.Text = "مجموع پرداختی " + dr[0].ToString() + " ریال";
            }

            Cnn.Close();

            ReservNumberTxt.Text = rs_id.ToString();
        }

        private void PaymentFRM_Load(object sender, EventArgs e)
        {
            SqlDataAdapter da;
            da = new SqlDataAdapter("select pt_id,pt_name from Payment_Type", Cnn);
            da.Fill(ds);
            PaymentTypeComboBox.DataSource = ds.Tables[0];
            PaymentTypeComboBox.DisplayMember = "pt_name";
            PaymentTypeComboBox.ValueMember = "pt_id";
        }

        private void PayTxt_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ReservNumberTxt.Text) && !string.IsNullOrEmpty(PaymentTypeComboBox.Text) && !string.IsNullOrEmpty(AmountToPayTxt.Text))
            {

                SqlCommand SaveINDB = new SqlCommand("insert into Payment (p_amount,p_code,p_date,p_time,p_f_id,p_type_id) values (@p_amount,@p_code,@p_date,@p_time,@p_f_id,@p_type_id)", Cnn);
                SaveINDB.Parameters.AddWithValue("@p_amount", Int64.Parse(AmountToPayTxt.Text));
                SaveINDB.Parameters.AddWithValue("@p_code", PaymentCodeTxt.Text);
                SaveINDB.Parameters.AddWithValue("@p_date", DateTime.Now.ToShortDateString());
                SaveINDB.Parameters.AddWithValue("@p_time", DateTime.Now.ToShortTimeString());
                SaveINDB.Parameters.AddWithValue("@p_f_id", f_id);
                SaveINDB.Parameters.AddWithValue("@p_type_id", (int)PaymentTypeComboBox.SelectedValue);

                Cnn.Open();
                SaveINDB.ExecuteNonQuery();
                Cnn.Close();

                /// update payed lbl
                /// 
                string TotalPayd;
                SqlCommand SelectTotalPayment = new SqlCommand("select ISNULL(SUM(p_amount),0) from Payment WHERE p_f_id=@p_f_id", Cnn);
                SelectTotalPayment.Parameters.AddWithValue("@p_f_id", f_id);
                Cnn.Open();
                dr = SelectTotalPayment.ExecuteReader();
                dr.Read();
                TotalPayedLbl.Text = "مجموع پرداختی " + dr[0].ToString() + " ریال";
                TotalPayd = dr[0].ToString();
                Cnn.Close();


                /// update total payed in data base
                SqlCommand UpdateCMD = new SqlCommand("UPDATE Factor SET f_payment=@f_payment WHERE f_id=@f_id", Cnn);

                UpdateCMD.Parameters.AddWithValue("@f_payment", Int64.Parse(TotalPayd));
                UpdateCMD.Parameters.AddWithValue("@f_id", f_id);

                Cnn.Open();
                UpdateCMD.ExecuteNonQuery();
                Cnn.Close();


                //
                SqlCommand SelectTPrice = new SqlCommand("select (f_tprice - IsNull((f_payment),0)) from Factor where f_rs_id = @f_rs_id", Cnn);
                SelectTPrice.Parameters.AddWithValue("@f_rs_id", rs_id);
                Cnn.Open();
                dr = SelectTPrice.ExecuteReader();
                if (dr.HasRows)
                {
                    dr.Read();
                    RemainingAmountLbl.Text = "مبلغ فابل پرداخت " + dr[0].ToString() + "ریال";
                }
                else
                {
                    MessageBox.Show("هزینه ای ثبت نشده است !!!");
                }

                Cnn.Close();

            }

            else
            {
                MessageBox.Show("لطفا همه قسمت ها را تکمیل نمایید");
            }

        }
    }
}
